package com.example.freemarkerDemo.controller;


import com.example.freemarkerDemo.domain.bo.UserBo;
import com.example.freemarkerDemo.domain.vo.UserVo;
import com.example.freemarkerDemo.service.UserService;
import com.example.freemarkerDemo.utils.web.core.BaseController;
import com.example.freemarkerDemo.utils.web.core.page.PageQuery;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.*;

/**
 * @version 1.0
 * @author: 枫玥筱筱
 * @date: 2024/3/26 15:57
 * 描述：
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/index")
public class indexController extends BaseController {
    //private final UserService userService;
    @Resource
    private UserService userService;

    /**
     * 访问路径：localhost:8080/f1
     * @return 返回给页面的数据
     */
    @GetMapping("/f1")
    public String test1(Model map) {
        //布尔类型
        map.addAttribute("flag", true);

        //日期类型
        map.addAttribute("date", new Date());

        //数字类型
        map.addAttribute("a",5.5474);
        map.addAttribute("b",18);
        map.addAttribute("c",10000);

        //字符串
        map.addAttribute("t1","Hello");
        map.addAttribute("t2", "world");
        map.addAttribute("t3", "hello world");

        // 空字符串
        map.addAttribute("test1", "");
        map.addAttribute("test2", null);

        //数组类型
        String[] st = {"北京","上海","广东","深圳"};
        map.addAttribute("st",st);

        //集合类型
        List<String> cityList = new ArrayList<>();
        cityList.add("北京");
        cityList.add("上海");
        cityList.add("广东");
        cityList.add("深圳");
        map.addAttribute("cityList",cityList);

        //map类型
        Map<String,Object> map2 = new HashMap<>();
        map2.put("name", "张三");
        map2.put("age", 22);
        map2.put("sex", "男");
        map.addAttribute("map",map2);


        //跳转页面
        return "index";
    }

    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("name", "赵四");
        return "test";
    }

    @GetMapping("/userList")
    public String userList(Model model, UserBo userBo, PageQuery pageQuery) {
        List<UserVo> list = userService.queryPageList(userBo,pageQuery);
        System.out.println("list:"+list);
        model.addAttribute("userList", list);
        Map<String, Object> map = new HashMap<>();
        map.put("userList", list);
        model.addAttribute("map", map);
        String file = "http://www.runoob.com/images/pulpit.jpg";
        model.addAttribute("image", file);
        return "userList";
    }

    /**
     * 新增用户信息
     */
    @PostMapping("/user/add")
    public Boolean addUser(UserBo userBo) {
        return userService.insertUser(userBo);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/user/edit")
    public Boolean editUser(UserBo userBo) {
        return userService.updateUser(userBo);
    }

    /**
     * 删除用户信息
     */
    @PostMapping("/user/delete/{id}")
    public Boolean deleteUser(@PathVariable("id") Long id) {
        return userService.deleteUserById(id);
    }

}
